package cn.globalph.housekeeper.ui.dialog

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import cn.globalph.housekeeper.R

class StartServerDialog : BaseDialogFragment(){
    private lateinit var closeImage:ImageView
    private lateinit var dateTv:TextView
    private lateinit var startNowTv:TextView
    private lateinit var serverTypeContentTv:TextView
    private lateinit var nameContentTv:TextView
    private lateinit var startTimeContentTv:TextView

    private var serviceCategory:String? = null
    private var name:String? = null
    private var actualStartDate:String? = null
    private var date:String? = null

    var listener:StartServerLintener? = null

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_start_server,null)

        arguments?.let {
            serviceCategory = it.getString("service_category")
            name = it.getString("name")
            actualStartDate = it.getString("actual_start_date")
            date = it.getString("date")
        }

        serverTypeContentTv = view.findViewById(R.id.server_type_content_tv)
        nameContentTv = view.findViewById(R.id.name_content_tv)
        startTimeContentTv = view.findViewById(R.id.start_time_content_tv)
        dateTv = view.findViewById(R.id.date_tv)

        serverTypeContentTv.text = serviceCategory
        nameContentTv.text = name
        startTimeContentTv.text = actualStartDate
        dateTv.text = date

        closeImage = view.findViewById(R.id.close_image)
        closeImage.setOnClickListener{
            dismiss()
        }


        startNowTv = view.findViewById(R.id.start_right_now_btn)
        startNowTv.setOnClickListener {
            AlertDialog.Builder(context!!)
                .setTitle("提示")
                .setMessage("确定要开始本次服务吗")
                .setPositiveButton("确定"
                ) { _, _ ->
                    run {
                        listener?.onStartClick()
                        dismiss()
                    }
                }
                .setNegativeButton("取消",null)
                .create()
                .show()

        }

        var dialog = Dialog(context!!, R.style.Translucent_NoTitle)
        dialog.setContentView(view)
        val layoutParams = view.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels - 160
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
        view.layoutParams = layoutParams
        dialog.window!!.setGravity(Gravity.CENTER)
        return dialog
    }

    interface StartServerLintener{
        fun onStartClick()
    }
}